<template>
  <div class="manager_plate">
    <section class="manager_plate_header">
      <a-card>
        <a-form name="advanced_search" autocomplete="off" ref="searchFormRef" class="ant-advanced-search-form"
          :model="searchFormState">
          <a-row :gutter="24">
            <a-col :span="12">
              <a-form-item name="plateName" label="板块名称">
                <a-input v-model:value="searchFormState.plateName" placeholder="请输入板块名称" allowClear />
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-form-item name="remark" label="备注">
                <a-input v-model:value="searchFormState.remark" placeholder="请输入备注" allowClear />
              </a-form-item>
            </a-col>
            <a-col :span="24" style="text-align: right">
              <a-button type="primary" @click="handleSearch">查询</a-button>
              <a-button style="margin: 0 8px" @click="handleReset">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </a-card>
    </section>
    <section class="manager_plate_content">
      <a-card style="height: 100%">
        <a-row>
          <a-col :span="24">
            <a-button type="primary" @click="handleAdd">新增板块</a-button>
          </a-col>
        </a-row>
        <a-row style="margin-top: 8px;">
          <a-col :span="24">
            <a-table :dataSource="dataSource" :columns="columns" size="small" :pagination="false" :scroll="{ x: 1000 }">

              <template #bodyCell="{ column, record }">
                <a-space v-if="column.dataIndex === 'operation'">
                  <a-button type="link" @click="handleEdit(record)">编辑</a-button>
                  <a-popconfirm title="确定要删除这条数据吗？" ok-text="确定" cancel-text="取消" @confirm="handleDelete(record.id)">
                    <a-button type="link" danger>删除</a-button>
                  </a-popconfirm>

                </a-space>
              </template>
            </a-table>
          </a-col>
        </a-row>
      </a-card>
    </section>
    <footer class="manager_plate_footer">
      <a-card style="height: 100%;">
        <a-row align="middle">
          <a-col :span="24">
            <a-pagination :total="total" v-model:current="current" v-model:pageSize="pageSize" :showSizeChanger="true"
              showQuickJumper :show-total="total => `共 ${total} 条数据`" @change="onPageChange" />
          </a-col>
        </a-row>
      </a-card>
    </footer>
    <a-modal v-model:open="open" :title="title" @ok="handleOk" :confirm-loading="confirmLoading" :destroyOnClose="true"
      centered :maskClosable="false">
      <a-form :ref="formRef" :model="formState" autocomplete="off" @finish="onFinish" @finishFailed="onFinishFailed">
        <a-form-item label="板块名称" name="plateName">
          <a-input v-model:value="formState.plateName" allowClear :maxlength="20" showCount  placeholder="请输入板块名称"/>
        </a-form-item>
        <a-form-item label="板块备注" name="remark">
          <a-textarea v-model:value="formState.remark" :auto-size="{ minRows: 2, maxRows: 5 }" allowClear placeholder="请输入备注" />
        </a-form-item>
      </a-form>
    </a-modal>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue';
import { getPlateList, addPlate, editPlate, deletePlate } from '@/apis/plate';
const searchFormRef = ref();
const formRef = ref();
const dataSource = ref([]);
const searchFormState = ref({
  plateName: "",
  remark: "",
});
const formState = ref({
  plateName: "",
  remark: "",
});

const current = ref(1);
const total = ref(0);
const pageSize = ref(10);
const handleSearch = async () => {
  try {
    const { data={} } = await getPlateList({
      pageNum: current.value,
      pageSize: pageSize.value,
      plateName:searchFormState.value.plateName,
      remark:searchFormState.value.remark,
    });
    dataSource.value = data?.list;
    total.value = data?.total;
  } catch (error) {
    console.log(error, 'get-plate-list-error');
  }

};
const open = ref(false);
const title = ref('');
const confirmLoading = ref(false);
let handleType = '';

const handleAdd = () => {
  open.value = true;
  title.value = '新增板块';
  handleType = 'add';
  formState.value = {};

};
const handleEdit = (record) => {

  formState.value = record;
  open.value = true;
  title.value = '编辑板块';
  handleType = 'edit';
};

const handleDelete = async (id) => {
  await deletePlate(id);
  init();
};
const handleOk = async () => {
  confirmLoading.value = true;
  if (handleType === 'add') {
    await addPlate({
      ...formState.value,
    });
  } else {
    await editPlate({
      ...formState.value,
    });
  }
  init();
  open.value = false;
  confirmLoading.value = false;
  title.value = '';
};
const onPageChange = (page, Size) => {
  current.value = page;
  pageSize.value = Size;
  init();
};

const handleReset = () => {
  searchFormRef.value?.resetFields();
  init();
};
const init = async () => {
  try {
    const {data={}} = await getPlateList({
      pageNum: current.value,
      pageSize: pageSize.value,
    });
    dataSource.value = data?.list;
    total.value = data?.total;
  } catch (error) {
    console.log(error, 'get-plate-list-error');
  }
};

onMounted(() => {
  init();
});


const columns = [
  {
    title: '板块名称',
    dataIndex: 'plateName',
    width: 140,
    align: 'center'
  },
  {
    title: '备注',
    dataIndex: 'remark',
    width: 320,
    align: 'center',
    ellipsis:true
  },
  {
    title: '创建时间',
    dataIndex: 'createTime',
    width: 160,
    align: 'center'
  },
  {
    title: '更新时间',
    dataIndex: 'updateTime',
    width: 160,
    align: 'center'
  },
  {
    title: '操作',
    dataIndex: 'operation',
    fixed: 'right',
    align: 'center'
  },
]
</script>

<style lang="less" scoped>
.manager_plate {
  box-sizing: border-box;
  padding: 8px;
  display: flex;
  flex-direction: column;
  height: 100%;

  &_header {
    width: 100%;
    max-height: 200px;
  }

  &_content {
    margin-top: 8px;
    flex: 1;
    overflow: auto;
  }

  &_footer {
    margin-top: 8px;
    height: 80px;
  }
}
</style>
